package com.inch.web.controller;

import com.inch.web.util.CodeUtil;
import org.apache.commons.io.IOUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;


@Controller
@RequestMapping("/upload")
public class UploadController {
    //upload one or more files
    @RequestMapping("/uploadFiles")
    public ModelAndView upload(@RequestParam("file")MultipartFile[] files) throws IOException {
        ModelAndView modelAndView = new ModelAndView();
        String message = "";
        System.out.println("Upload Started.");
        message += "\nUpload Started.";
        long uploadStartTime = System.currentTimeMillis();
        for (MultipartFile file : files) {
            long startTime = System.currentTimeMillis();
            String originalFilename = file.getOriginalFilename();
            String contentType = file.getContentType();
            file.transferTo(new File("C:/D/upload/"+originalFilename));
            long endTime = System.currentTimeMillis();
            System.out.println("File:\t" + originalFilename + " uploaded.\tTime used:\t" + (endTime-startTime));
            message += "\nFile:\t" + originalFilename + " uploaded.\tTime used:\t" + (endTime-startTime);
        }
        long uploadEndTime = System.currentTimeMillis();
        System.out.println("Upload Ended. Total Time used:\t" + (uploadEndTime-uploadStartTime) +"ms");
        message += "\nUpload Ended. Total Time used:\t" + (uploadEndTime-uploadStartTime) + "ms";
        modelAndView.setViewName("redirect:uploadDownload");
        modelAndView.addObject("message", message);
        return modelAndView;
    }

    @RequestMapping("/uploadDownload")
    public ModelAndView uploadDownload(@RequestParam("message") String message){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("jsp/uploadDownload");
        modelAndView.addObject("message",message);
        return modelAndView;
    }

    @RequestMapping("/download")
    public ModelAndView download(@RequestParam("kaptcha") String kaptcha, HttpServletRequest request, HttpServletResponse response, HttpSession session) throws IOException {
        if(CodeUtil.checkVerifyCode(request,kaptcha)){
            String dir = "C:\\D\\upload";
            String filename = dir + "\\" + "hwmonitor_1.42.zip";
            //set response header
            response.setHeader("content-disposition","attachment;filename=hwmonitor_1.42.zip");
            IOUtils.copy(new FileInputStream(filename),response.getOutputStream());
            session.setAttribute("message","Verify succeed.");
            return null;
        } else {
            ModelAndView modelAndView = new ModelAndView();
            modelAndView.setViewName("jsp/uploadDownload");
            modelAndView.addObject("message","Verify Failed. Try again.");
            return modelAndView;
        }
    }
}
